package com.zzxx.wechat.service.api;


import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WechatService {


    @Autowired
    WxMpService wxMpService;

    public String sendTemplateMessage(String userId, String templateId,String url,List<WxMpTemplateData> wxMpTemplateDataList) throws WxErrorException {
        // 创建模板消息，设置模板id、指定模板消息要发送的目标用户
        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                .templateId(templateId)
                .toUser(userId)
                .url(url)
                .build();
        // 填充模板消息中的变量
        for (WxMpTemplateData wxMpTemplateData : wxMpTemplateDataList) {
            wxMpTemplateMessage.addData(wxMpTemplateData);
        }
        // 发送模板消息，返回消息id
        return wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
    }



}
